package com.lrs.java.jdbc.jdbc_exercise;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Types;
/**
 * 在Java中执行数据库的procedure。（非常牛逼）。
 * @author Administrator
 *
 */
public class Testprocedure {
	
	public static void main(String[] args) {
		Connection conn = null; 
		CallableStatement cstmt = null;
		
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		try {
			conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ZLPD", "scott", "tiger");
			cstmt = conn.prepareCall("{ call p(?, ?, ?, ?) }");
			cstmt.registerOutParameter(3, Types.INTEGER);
			cstmt.registerOutParameter(4, Types.INTEGER);
			
			cstmt.setInt(1, 3);
			cstmt.setInt(2, 4);
			cstmt.setInt(4, 0);
			
			cstmt.execute();
			
			System.out.println(cstmt.getInt(3));
			System.out.println(cstmt.getInt(4));
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if(cstmt != null) {
					cstmt.close();
					cstmt = null;
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				if(conn != null) {
					conn.close();
					conn = null;
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	}
}
